package com.system.healthproject.model.admin.service.impl;

import com.system.healthproject.bean.DepartmentBean;
import com.system.healthproject.bean.ResultAllHospitalDepartmentIdName;
import com.system.healthproject.bean.ResultDepartmentIdName;
import com.system.healthproject.bean.ParamUpdateDepartment;
import com.system.healthproject.mapper.DepartmentMapper;
import com.system.healthproject.mapper.DoctorMapper;
import com.system.healthproject.model.admin.service.DepartmentService;
import com.system.healthproject.pojo.Department;
import com.system.healthproject.pojo.Doctor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 根据医院ID查询科室ID、科室名称
     * @param hospitalId
     * @return
     */
    @Override
    public List<ResultDepartmentIdName> selectHospitalIdName(Integer hospitalId) {
        return departmentMapper.selectHospitalIdName(hospitalId);
    }

    /**
     * 查询所有医院ID、名称  医院对应的所有科室ID、科室名称
     * @return
     */
    @Override
    public List<ResultAllHospitalDepartmentIdName> selectAllHospitalDepartmentIdName() {
        return departmentMapper.selectAllHospitalDepartmentIdName();
    }

    /**
     * 新增科室
     * @param departmentName
     * @param departmentParentid
     * @param departmentUser
     * @param departmentPhone
     * @return
     */
    @Override
    public String addDepartment(Integer hospitalId,String departmentName, Integer departmentParentid, String departmentUser, String departmentPhone) {
        return departmentMapper.addDepartment(hospitalId,departmentName,departmentParentid,departmentUser,departmentPhone,new Date())>0?"成功":"失败";
    }

    /**
     * 修改科室信息
     * @param hospitalId
     * @param departmentId
     * @param departmentName
     * @param departmentParentid
     * @param departmentUser
     * @param departmentPhone
     * @return
     */
    @Override
    public String updateDepartment(Integer hospitalId, Integer departmentId, String departmentName, Integer departmentParentid,
                                   String departmentUser, String departmentPhone,String createTime) {
        ParamUpdateDepartment updateDepartment = new ParamUpdateDepartment();
        updateDepartment.setHospitalId(hospitalId);
        updateDepartment.setDepartmentId(departmentId);
        updateDepartment.setDepartmentName(departmentName);
        updateDepartment.setDepartmentParentid(departmentParentid);
        updateDepartment.setDepartmentUser(departmentUser);
        updateDepartment.setDepartmentPhone(departmentPhone);
        if(createTime!=null && !createTime.equals("")){
            try {
                updateDepartment.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(createTime));
            } catch (ParseException e) {
                updateDepartment.setCreateTime(null);
                e.printStackTrace();
            }
        }updateDepartment.setCreateTime(null);
        return departmentMapper.updateDepartment(updateDepartment)>0?"成功":"失败";
    }

    /**
     * 根据医院ID查询所有科室列表
     */
    @Override
    public List<DepartmentBean> selectHospitalIdDepartment(Integer hospitalId, Integer page, Integer limit) {
        return departmentMapper.selectHospitalIdDepartment(hospitalId,page,limit);
    }

    @Override
    public Integer countDepartmentNumber(Integer hospitalId) {
        return departmentMapper.countDepartmentNumber(hospitalId);
    }

    /**
     * 根据医院ID和科室ID查询科室详细信息
     * @param hospitalId
     * @param departmentId
     * @return
     */
    @Override
    public Department selectHospitalIdDepartmentId(Integer hospitalId, Integer departmentId) {
        return departmentMapper.selectHospitalIdDepartmentId(hospitalId,departmentId);
    }

    /**
     * 根据科室ID删除科室
     * @param departmentId
     * @return
     */
    @Override
    public String deleteDepartment(Integer departmentId) {
        //查询科室中的所有医生
        List<Doctor> list = doctorMapper.selectrdepIdAllDoctor(departmentId);
        if(list!=null && list.size()>0){
            return "科室中存在医生，不能删除";
        }else{
            return departmentMapper.deleteDepartment(departmentId)>0?"成功":"失败";
        }
    }
}
